home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3664 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: crl.crl.com!not-for-mail
  2. From: bobfry@crl.com (Robert Fry)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Simple i/o for my class
  5. Date: 30 Jan 1996 08:05:38 -0800
  6. Organization: CRL Dialup Internet Access
  7. Message-ID: <4elfki$jnd@crl.crl.com>
  8. References: <4ekjql$5jl@nnrp1.news.primenet.com>
  9. NNTP-Posting-Host: crl.com
  10.  
  11. mcoplea@primenet.com (Marty R. Coplea) writes:
  12.  
  13. <snipping most of the question>
  14.  
  15. >It seems to take the input OK, but always returns a '0'
  16.  
  17. >int
  18. >main(void)
  19. >{
  20. >    float avar, hvar;      /*float variables(avar=At-bats,hvar=Hits */
  21. >    int Hitsint, Atbatsint;       /* Integers with Hits and At Bats */
  22. >    float Avg;           /* Results Batting Average */
  23.  
  24. >    /* Prompt the user to input the data */
  25. >    printf("Enter the number of at-bats followed by hits:   ");
  26. >    scanf("%f %f", &avar, &hvar);
  27.  
  28. >    /* Convert to integers */
  29. >    Atbatsint = avar;
  30. >    Hitsint = hvar;
  31.  
  32. >    /* Calculate the Batting Average */
  33. >    Avg = Hitsint/Atbatsint;
  34.  
  35. This line probably doesn't do what you're expecting. If you look at it 
  36. with a debugger, you'll notice that Avg gets assigned the value '0', 
  37. here. The problem is that you're doing integer arithmetic (and here's a 
  38. problem for you to solve for yourself: why?). You can either get rid of 
  39. the integer variables (since they do little but clutter the program) or 
  40. cast the dividend and divisor to float before calculating the average.
  41.  
  42. <skipping the rest>
  43.  
  44. I hope this helps.
  45.   Bob
  46.